<?php
class Core_Common_Sendmail {
	private $_smtpAdapter;
	private $_sender;
	private $_urlvalidator;
	private $_pathmail;
	private $_enable;
	public function __construct() {
		$sender=Zend_Controller_Front::getInstance()->getParam('bootstrap')->getOption('smtp');
		$mailvalidator = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getOption('mailvalidator');
		$this->_urlvalidator = $mailvalidator['postvalidate'];
		$this->_pathmail = $mailvalidator['mailpath'];
		$this->_enable = $mailvalidator['enable'];
		$this->_smtpAdapter = new Zend_Mail_Transport_Smtp($sender['host'],array("port"=>$sender['port']));
		$this->_sender = $sender['sender'];
	}
	
	public function sendConfirm($user) {
		if(!$user->Validated) {
			try {
				$filename = $this->_pathmail."/validatemail.html";
				$token = sha1($user->Email.$user->Lastname); 
				if(file_exists($filename)) {
					$filehandle = fopen($filename, "r");
					$htmlbody = fread($filehandle,filesize($filename));
					$htmlbody = str_replace('[validatelink]', $this->_urlvalidator.$token, $htmlbody);
					$email = new Zend_Mail();
					$email->setBodyHtml($htmlbody);
					$email->setFrom($this->_sender);
					$email->addTo($user->Email);
					$email->setSubject("Comfirmer votre compte Lemeilleurestavenir");
					if($this->_enable==1)
						$email->send($this->_smtpAdapter);
				}
			}
			catch (Exception $e) {
				$log = Zend_Log::factory(Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('log'));
				$log->log("SendMailError, class Core_Common_Sendmail".$e->Messages, Zend_Log::DEBUG);
			}
		}
	}
}
?>
